<?php

namespace App\Task\Update;

use App\Model\User;
use App\Model\UserCancelLog;
use Hyperf\Crontab\Annotation\Crontab;

/**
 * 定时注销用户（用户注销申请）
 * @Crontab(name="AutoCancelAccountTask", rule="55 1 * * *", callback="execute", memo="定时执行用户注销申请", singleton=false)
 */
class AutoCancelAccountTask
{
    public function execute()
    {
        log_info(__CLASS__ . "==Start==" . date('Y-m-d H:i:s'));
        $expDays = env('AUTO_CANCEL_DAY', 15);
        $userIds = UserCancelLog::getLogs(UserCancelLog::FLAG_ING, date('Y-m-d', strtotime("-{$expDays} day")));
        if (empty($userIds)) return;
        foreach ($userIds as $userId) {
            //更新状态
            $ret = UserCancelLog::where('user_id', $userId)->update(['flag' => UserCancelLog::FLAG_OK]);
            //注销账号
            User::cancelAccount($userId);
            log_user_result(__CLASS__ . "==UserId:{$userId}");
        }
        return;
    }
}
